package com.grudesign.stutrip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grudesign.stutrip.common.ReturnInfo;
import com.grudesign.stutrip.entity.Po.AdminInfoPo;
import com.grudesign.stutrip.entity.Vo.adminQuery;
import com.grudesign.stutrip.mapper.AdminInfoMapper;
import com.grudesign.stutrip.service.AdminInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author wx
 * @since 2022-05-09
 */
@Service
public class AdminInfoServiceImpl extends ServiceImpl<AdminInfoMapper, AdminInfoPo> implements AdminInfoService {

    @Override
    public Object addAdmin(AdminInfoPo adminInfoPo) {
        baseMapper.insert(adminInfoPo);
        return new ReturnInfo("200", "ok");
    }

    @Override
    public Object getQueryAdmin(adminQuery adminQuery) {
        String account = adminQuery.getAccount();
        QueryWrapper<AdminInfoPo> adminWrapper = Wrappers.query();
        if(StringUtils.hasText(account)){
            adminWrapper.like("account",account);
        }
        List<AdminInfoPo> adminInfoPos = baseMapper.selectList(adminWrapper);
        return new ReturnInfo("200","ok").data("admins",adminInfoPos);
    }

    @Override
    public Object login(AdminInfoPo adminInfoPo) {
        String username =adminInfoPo.getAccount();
        String pwd = adminInfoPo.getPassword();
        QueryWrapper<AdminInfoPo> adminInfoPoQueryWrapper = Wrappers.query();
        adminInfoPoQueryWrapper.eq("account",username);
        AdminInfoPo admin = baseMapper.selectOne(adminInfoPoQueryWrapper);
        if(!admin.getPassword().equals(pwd)){
            return new ReturnInfo("-1","error pwd!");
        }
        return new ReturnInfo("200","ok").data("admin",admin);
    }
}
